using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using SeeqSharp;
using System.Drawing.Drawing2D;

namespace SeeqDownloader
{
    class DownloadsList : FlickerFreeListBox
    {
        public DownloadsList()
            : base()
        {
            UpdateItemHeight();
        }

        float m_textY = 0;

        void UpdateItemHeight()
        {
            float fontHeight = Font.GetHeight();
            base.ItemHeight = (int)(fontHeight + 10);
            m_textY = ItemHeight / 2 - fontHeight / 2;
        }

        public override Font Font
        {
            get
            {
                return base.Font;
            }
            set
            {
                base.Font = value;
                UpdateItemHeight();
            }
        }

        public override int ItemHeight
        {
            get
            {
                return base.ItemHeight;
            }
            set
            {
                UpdateItemHeight();
            }
        }

        static readonly Color RECTANGLE_COLOR = Color.LightGray;
        static readonly Color GRADIENT_TOP_COLOR = Color.FromArgb(0x55, 0xff, 0x5e);
        static readonly Color GRADIENT_BOTTOM_COLOR = Color.FromArgb(0xc5, 0xff, 0xbe);

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            if ( (e.Index < 0) || (e.Index >= Items.Count) )
                return;

            DownloadListItem item =(DownloadListItem)Items[e.Index];

            e.DrawBackground();

            Pen rectanglePen = new Pen(RECTANGLE_COLOR);
            e.Graphics.DrawLine(rectanglePen, e.Bounds.X + 2, e.Bounds.Y + 1, e.Bounds.Right - 3, e.Bounds.Y + 1);
            e.Graphics.DrawLine(rectanglePen, e.Bounds.X + 2, e.Bounds.Bottom - 2, e.Bounds.Right - 3, e.Bounds.Bottom - 2);
            e.Graphics.DrawLine(rectanglePen, e.Bounds.X + 1, e.Bounds.Y + 2, e.Bounds.X + 1, e.Bounds.Bottom - 3);
            e.Graphics.DrawLine(rectanglePen, e.Bounds.Right - 2, e.Bounds.Y + 2, e.Bounds.Right - 2, e.Bounds.Bottom - 3);

            Rectangle rect = new Rectangle(
                e.Bounds.X+2,
                e.Bounds.Y+2,
                e.Bounds.Width-4,
                e.Bounds.Height-4);
            e.Graphics.FillRectangle(new SolidBrush(BackColor), rect);

            rect.Width = (int)Math.Ceiling((e.Bounds.Width - 4) * item.Progress);
            if (rect.Width != 0)
            {
                LinearGradientBrush brush = new LinearGradientBrush(rect, GRADIENT_TOP_COLOR, GRADIENT_BOTTOM_COLOR, 90);
                e.Graphics.FillRectangle(brush, rect);
            }

            e.Graphics.DrawString(item.Track.ToString(), Font, new SolidBrush(ForeColor), e.Bounds.X + 4, e.Bounds.Y + m_textY);
        }
    }
}
